home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3565 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Floating point calculation order
  5. Date: Mon, 29 Jan 96 23:11:58 GMT
  6. Organization: none
  7. Message-ID: <822957118snz@genesis.demon.co.uk>
  8. References: <m0tedv8-0002eqC@sice.nsk.su> <3104c6d9.134061184@nntp.ix.netcom.com> <DLnE5K.2xH@microunity.com> <822428038snz@genesis.demon.co.uk> <4e8dg1$ehg@ns.RezoNet.NET>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4e8dg1$ehg@ns.RezoNet.NET> ray@ultimate-tech.com "Ray Dunn" writes:
  15.  
  16. >In referenced article, Lawrence Kirby says...
  17. >>Parentheses in C allow you to change the operator/operand grouping 
  18. >>within expressions. They don't enforce any order of evaluation beyond 
  19. >>that implied by the grouping. So:
  20. >>
  21. >> x = p * q / r;
  22. >>
  23. >>and
  24. >>
  25. >> x = (p * q) / r;
  26. >>
  27. >>mean *exactly* the same thing in C since the parentheses don't change 
  28. >>the grouping.
  29. >
  30. >You're right, of course, but they only mean the same thing because * 
  31. >and / have the same precedence and associate left to right.
  32. >
  33. >Remember that the original posting was from someone complaining that 
  34. >the division was done first (presumably on a non-conforming compiler). 
  35. >In this case parenthesizing the multiplication would indeed change the 
  36. >order of evaluation and solve his problem.
  37.  
  38. There are two possibilities here:
  39.  
  40. 1. The compiler got the precedence/associativity rules wrong and the
  41.    parentheses would make a difference.
  42.  
  43. 2. The compiler got the precedence/associativity right but decided to
  44.    reorder as an 'optimisation' based on the mathematical equivalence of
  45.    the 2 orderings. Here the parentheses are unlikely to make a difference.
  46.  
  47. I think 2 is by far the more likely possibility.
  48.  
  49. >I do think it's a good idea to parenthesize expressions to make it 
  50. >clear what you expect to happen.
  51.  
  52. Agreed, although excessive parenthesization can be as bad (worse?) than too
  53. little.
  54.  
  55. -- 
  56. -----------------------------------------
  57. Lawrence Kirby | fred@genesis.demon.co.uk
  58. Wilts, England | 70734.126@compuserve.com
  59. -----------------------------------------
  60.